package cn.lll.vuehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.dto.CustomerPageDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lll.vuehouse.entity.Customer;
import cn.lll.vuehouse.service.CustomerService;
import cn.lll.vuehouse.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author asus-pc
 * @description 针对表【bus_customer】的数据库操作Service实现
 * @createDate 2023-09-01 11:34:05
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
        implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ResponseResult getCustomerPage(CustomerPageDTO customerPageDTO) {
        ResponseResult responseResult = new ResponseResult();
        // 客户分页查询，搜索
        LambdaQueryWrapper<Customer> lambda = new QueryWrapper<Customer>().lambda();
        lambda.like(StringUtils.isNotBlank(customerPageDTO.getCustomername()), Customer::getCustomername, customerPageDTO.getCustomername())
                .like(StringUtils.isNotBlank(customerPageDTO.getConnectionpersion()), Customer::getConnectionpersion, customerPageDTO.getConnectionpersion())
                .eq(ObjectUtil.isNotEmpty(customerPageDTO.getAvailable()), Customer::getAvailable, customerPageDTO.getAvailable())
                .orderByDesc(Customer::getId);
        Page<Customer> customerPage = new Page<>(customerPageDTO.getCurrentPage(), customerPageDTO.getLimit());
        customerMapper.selectPage(customerPage, lambda);
        List<Customer> records = customerPage.getRecords();
        long total = customerPage.getTotal();
        HashMap<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("currentPageData", records);
        responseResult.success(ResponseStatus.SUCCESS, result);
        return responseResult;
    }

    @Override
    public ResponseResult getCustomerById(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        Customer customer = customerMapper.selectById(id);
        responseResult.success(ResponseStatus.SUCCESS, customer);
        return responseResult;
    }

    @Override
    public ResponseResult updateUser(Customer customer) {
        ResponseResult responseResult = new ResponseResult();
        customerMapper.updateById(customer);
        responseResult.success();
        return responseResult;
    }

    @Override
    public ResponseResult addUser(Customer customer) {
        ResponseResult responseResult = new ResponseResult();
        // 判断客户是否存在
        String customername = customer.getCustomername();
        LambdaQueryWrapper<Customer> lambda = new QueryWrapper<Customer>().lambda();
        lambda.eq(Customer::getCustomername,customername);
        Customer customer1 = customerMapper.selectOne(lambda);
        if (ObjectUtil.isNotNull(customer1)){
            responseResult.fail(ResponseStatus.CUSTOMER_EXIST);
            return responseResult;
        }
        customerMapper.insert(customer);
        responseResult.success();
        return responseResult;
    }
}




